package com.CST.aspect;

import com.CST.annotation.*;
import com.CST.common.api.*;
import java.lang.reflect.*;
import org.aspectj.lang.*;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.*;
import org.springframework.core.annotation.*;
import org.springframework.stereotype.*;
import org.springframework.util.*;

/**
 * @Description:
 * @Author: wzw
 * @Time: 2020/4/3 17:19
 */
@Aspect
@Component
@Order(1)
public class ReturnAspect {

    /**
     * 返回相应结果
     *
     * @param point 切面
     * @return
     * @throws Throwable
     */
    @Around("@annotation(com.CST.annotation.ResultObjectAnnotation)")
    public Object returnObject(ProceedingJoinPoint point) throws Throwable {
	  final Object proceed = point.proceed();
	  final MethodSignature signature = (MethodSignature) point.getSignature();
	  final Method method = signature.getMethod();
	  final ResultObjectAnnotation annotation = method.getAnnotation(
		ResultObjectAnnotation.class);
	  final String name = annotation.value().name();
	  final String successMessage = annotation.successMessage();
	  final String errorMessage = annotation.errorMessage();

	  //返回相应的数据
	  switch (name) {
		case "SELECT": {
		    return ResultObject.success(proceed);
		}
		case "INSERT": {
		    if (proceed instanceof Boolean) {
			  return (boolean) proceed ? ResultObject.success(
				returnMessage(successMessage, "添加成功")) : ResultObject.failed(
				returnMessage(errorMessage, "添加失败"));
		    }
		    if (proceed instanceof Integer) {
			  return (Integer) proceed > 0 ? ResultObject.success(
				returnMessage(successMessage, "添加成功")) : ResultObject.failed(
				returnMessage(errorMessage, "添加失败"));
		    }
		    if (proceed instanceof String) {
			  return ResultObject.success(returnMessage(successMessage, "添加成功"));
		    }
		    if (proceed instanceof ResultObject) {
			  return proceed;
		    }
		    if (proceed instanceof Response) {
			  return proceed;
		    }

		}
		case "UPDATE": {
		    if (proceed instanceof Boolean) {
			  return (boolean) proceed ? ResultObject.success(
				returnMessage(successMessage, "更新成功")) : ResultObject.failed(
				returnMessage(errorMessage, "更新失败"));
		    } else if (proceed instanceof Integer) {
			  return (Integer) proceed > 0 ? ResultObject.success(
				returnMessage(successMessage, "更新成功")) : ResultObject.failed(
				returnMessage(errorMessage, "更新失败"));
		    } else if (proceed instanceof ResultObject) {
			  return proceed;
		    } else if (proceed instanceof Response) {
			  return proceed;
		    } else {
			  return proceed;
		    }
		}

		case "DELETE": {
		    if (proceed instanceof Boolean) {
			  return (boolean) proceed ? ResultObject.success(
				returnMessage(successMessage, "删除成功")) : ResultObject.failed(
				returnMessage(errorMessage, "删除失败"));
		    }
		    if (proceed instanceof Integer) {
			  return (Integer) proceed > 0 ? ResultObject.success(
				returnMessage(successMessage, "删除成功")) : ResultObject.failed(
				returnMessage(errorMessage, "删除失败"));
		    }
		    if (proceed instanceof ResultObject) {
			  return proceed;
		    }
		    if (proceed instanceof Response) {
			  return proceed;
		    }
		}

		default: {
		    final String methodName = method.getName();
		    return new IllegalAccessError("在‘" + methodName + "’打上ResultObjectAnnotation注解");
		}

	  }

    }

    public String returnMessage(String isMessage, String message) {
	  return StringUtils.isEmpty(isMessage) ? message : isMessage;
    }
}

